﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ModelDinhMuc.Model;
namespace TinhNguyenVatLieuSanBay
{
    public partial class FrmPhieuDungCu : Form
    {
        DINHMUCSANBAYDB db = new DINHMUCSANBAYDB();
        public static FrmPhieuDungCu frmphieudungcu = null;
        private IList<DungCuPhucVuChuyenBay> lstphieudungcu;
        private ChuyenBay currentchuyenbay;
        private bool themorcapnhat = false;
        private long idchuyenbay;
        public FrmPhieuDungCu()
        {
            InitializeComponent();
            frmphieudungcu = this;
            InitPhieuDungCu();
            InitCatalog();
        }

        private void InitPhieuDungCu()
        {
            lstphieudungcu = db.SelectAllDungCuPhucVuChuyenBay().ExecuteTypedList<DungCuPhucVuChuyenBay>();
            if (lstphieudungcu != null && lstphieudungcu.Count > 0)
            {
                dungCuPhucVuChuyenBayBindingSource.DataSource = lstphieudungcu;
            }
        }

        private void InitCatalog()
        {
            IList<Catalog> lstcatalog;
            lstcatalog = db.selectcatalogHaveParent(2).ExecuteTypedList<Catalog>();
            catalogBindingSource.DataSource = lstcatalog;
            catalogBindingSource1.DataSource = lstcatalog;
        }

        private void InsertUpdatePhieuDichVu()
        {
            IList<DungCuPhucVuChuyenBay> lst;
            string tenphieudichvu=txttendungcu.Text;
            long idcatalog = long.Parse(cmbcatalog.SelectedValue.ToString());
            if (themorcapnhat == true)
            {
                lst = db.InsertUpdateDungCuPhucvuChuyenBay(0, tenphieudichvu, idchuyenbay, idcatalog).ExecuteTypedList<DungCuPhucVuChuyenBay>();
                if (lst != null && lst.Count > 0)
                {
                    InitPhieuDungCu();
                    int count = lstphieudungcu.Count;
                    dungCuPhucVuChuyenBayBindingSource.Position = count;
                }
            }
            else
            {
                long idphieudungcu = long.Parse(gvphieudungcu.CurrentRow.Cells["iDDataGridViewTextBoxColumn"].Value.ToString());
                lst = db.InsertUpdateDungCuPhucvuChuyenBay(idphieudungcu, tenphieudichvu,0,idcatalog).ExecuteTypedList<DungCuPhucVuChuyenBay>();
                if (lst != null && lst.Count > 0)
                {
                    InitPhieuDungCu();
                }
            }
            themorcapnhat = false;
        }

        private void DeletePhieuDichVu()
        {
            try
            {
                long idphieudungcu = long.Parse(gvphieudungcu.CurrentRow.Cells["iDDataGridViewTextBoxColumn"].Value.ToString());
                db.DeleteDungCuPhucVuChuyenBay(idphieudungcu).Execute();
                InitPhieuDungCu();
            }
            catch
            {
            }

        }


        private void FrmPhieuDungCu_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmphieudungcu = null;
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            themorcapnhat = true;
            txttendungcu.Clear();
            txttendungcu.Focus();
            txtmamaybay.Clear();
            txtmamaybay.Enabled=true;
        }

        private void txtmamaybay_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                FrmTimKiemChuyenBay frm = new FrmTimKiemChuyenBay(txtmamaybay.Text.ToUpper().Trim());
                frm.ShowDialog();
                currentchuyenbay = frm.chuyenbay;
                txtmamaybay.Text = currentchuyenbay.MaMB;
                txtmachuyenbay.Text = currentchuyenbay.MaBAY;
                idchuyenbay = currentchuyenbay.ID;
            }
        }

        private void btnluu_Click(object sender, EventArgs e)
        {
            InsertUpdatePhieuDichVu();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DeletePhieuDichVu();
        }

        private void txttimkiem_TextChanged(object sender, EventArgs e)
        {
            IList<DungCuPhucVuChuyenBay> lsttmp;
            lsttmp = lstphieudungcu.Where(x => x.TenPhieuDungCu.Contains(txttimkiem.Text)).ToList();
            dungCuPhucVuChuyenBayBindingSource.DataSource = lsttmp;
        }

        private void gvphieudungcu_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            var item = gvphieudungcu.CurrentRow.DataBoundItem as DungCuPhucVuChuyenBay;
            if (item.IDCatalog != null)
            {
                cmbcatalog.SelectedValue = item.IDCatalog;
            }
        }

        private void cmbfilltercatalog_SelectedValueChanged(object sender, EventArgs e)
        {
            try
            {
                IList<DungCuPhucVuChuyenBay> lsttmp;
                long idcatlog=long.Parse(cmbfilltercatalog.SelectedValue.ToString());
                lsttmp = lstphieudungcu.Where(x => x.IDCatalog == idcatlog).ToList();
                if (lsttmp != null && lsttmp.Count > 0)
                {
                    dungCuPhucVuChuyenBayBindingSource.DataSource = lsttmp;
                }
            }
            catch
            {

            }
        }
    }
}
